namespace EyeOpen.DataAnnotationsSample.Data.Nhibernate
{
	using System;

	using EyeOpen.Data;
	using EyeOpen.Data.Nhibernate;
	using EyeOpen.DataAnnotationsSample.Data;
	using EyeOpen.Domain;

	using NHibernate;

	public class AccountingUnitOfWork : IAccountingUnitOfWork
	{
		private readonly ISession session;

		public AccountingUnitOfWork(ISession session)
		{
			this.session = session;
		}

		public IRepository<Customer> CustomerRepository
		{
			get
			{
				return new NhibernateRepository<Customer>(session);
			}
		}

		/// <exception cref="Exception"></exception>
		public void Commit()
		{
			using (var transaction = session.BeginTransaction())
			{
				try
				{
					transaction.Commit();
				}
				catch
				{
					transaction.Rollback();
					throw;
				}
			}
		}

		public void Dispose()
		{
			session.Dispose();
		}
	}
}